package vue;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.CubicCurve2D;

import javax.swing.JPanel;

import model.Courbe;
import model.DefaultModel;
import model.PointControl;

public class AireDePoint extends JPanel {

	private static final long serialVersionUID = 7928760315893132338L;
	private DefaultModel model;

	public AireDePoint(DefaultModel model) {
		this.model = model;
	}

	@Override
	public Dimension getPreferredSize() {
		return new Dimension(300, 300);
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		final Graphics2D g2 = (Graphics2D) g;
		PointControl p = null;
		int t = DefaultModel.sideLength;
		CubicCurve2D.Double cubicCourbe = new CubicCurve2D.Double();

		for (Courbe courbe : model) {
			for (PointControl point : courbe) {
				g2.drawRect((int) point.getX() - t / 2, (int) point.getY() - t
						/ 2, t, t);
				if (p != null) {
					cubicCourbe.setCurve(p.getX(), p.getY(), p.getX()
							- p.getCtrlX(), p.getY() - p.getCtrlY(), point
							.getX()
							+ point.getCtrlX(),
							point.getY() + point.getCtrlY(), point.getX(),
							point.getY());
					g2.draw(cubicCourbe);
				}
				if (model.isCurrentTangentePoint(point)){
					g2.drawLine((int)(point.getX()
							), (int)(point.getY() ), (int)(point
							.getX()
							+ point.getCtrlX()),(int)(
							point.getY() + point.getCtrlY()));
					g2.drawLine((int)(point.getX()
					), (int)(point.getY() ), (int)(point
					.getX()
					- point.getCtrlX()),(int)(
					point.getY() - point.getCtrlY()));
					g2.drawRect((int)(point
							.getX()
							+ point.getCtrlX())- t / 2,(int)(
							point.getY() + point.getCtrlY())- t / 2,t,t);
					g2.drawRect((int)(point
							.getX()
							- point.getCtrlX())- t / 2,(int)(
							point.getY() - point.getCtrlY())- t / 2,t,t);
				}
				p = point;
			}
			p = null;
		}
	}
}
